package jp.co.htkk.repository.impl;

import jp.co.htkk.common.CommonUtil;
import jp.co.htkk.dto.${TableName!}Dto;
import jp.co.htkk.dto.UserDto;
import jp.co.htkk.entity.${TableName!};
import jp.co.htkk.mapper.${TableName!}Mapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;

@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
@Transactional
@Rollback(value = true)
class ${TableName!}RepositoryImplTest {
    @Autowired
    private ${TableName!}RepositoryImpl ${tableName!}Repository;
    @SpyBean
    private ${TableName!}Mapper ${tableName!}Mapper;

    @Test
    void create${TableName!}() {
        CommonUtil commonUtil = new CommonUtil();
        ${TableName!} ${tableName!} = new ${TableName!}();
        ${tableName!}.set${TableName!}Id("test${tableName!}id");
        ${tableName!}.setUserId("testuserid");
        ${tableName!}.setName("testname");
        ${tableName!}.setGender(0);
        ${tableName!}.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}.setDeleteFlag(0);
        ${tableName!}.setRegisterDateTime(new Date());
        ${tableName!}.setUpdateDateTime(new Date());
        ${tableName!}.setBirthDate("20200202");
        int effectRows = ${tableName!}Repository.create${TableName!}(${tableName!});
        assertTrue(effectRows > 0);
    }


    @Test
    void get${TableName!}ById() {
        ${TableName!}Dto ${tableName!}Dto = new ${TableName!}Dto();
        ${tableName!}Dto.setId(1l);
        ${tableName!}Dto.set${TableName!}Id("1");
        ${tableName!}Dto.setUserId("1");
        ${tableName!}Dto.setName("test");
        ${tableName!}Dto.setGender(0);
        ${tableName!}Dto.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}Dto.setBirthDate("20200202");

        doReturn(${tableName!}Dto).when(${tableName!}Mapper).get${TableName!}ById(any());
        ${TableName!}Dto ${tableName!} = ${tableName!}Repository.get${TableName!}ById("1");
        assertNotNull(${tableName!});

    }

    @Test
    void delete${TableName!}ById() {
        doReturn(1).when(${tableName!}Mapper).delete${TableName!}ById(any());
        int effectRows = ${tableName!}Repository.delete${TableName!}ById("1");
        assertTrue(effectRows>0);
    }

    @Test
    void update${TableName!}() {
        ${TableName!}Dto ${tableName!}Dto = new ${TableName!}Dto();
        ${tableName!}Dto.setId(1l);
        ${tableName!}Dto.set${TableName!}Id("1");
        ${tableName!}Dto.setUserId("1");
        ${tableName!}Dto.setName("test");
        ${tableName!}Dto.setGender(0);
        ${tableName!}Dto.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}Dto.setBirthDate("20200202");

        doReturn(1).when(${tableName!}Mapper).update${TableName!}(any());
        int effectRows = ${tableName!}Repository.update${TableName!}(${tableName!}Dto);
        assertTrue(effectRows > 0);
    }
}
